<?php

namespace app\admin\controller;

use think\Cache;
use think\Controller;
use think\Request;

class MerchantMenu extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return $this->fetch('',['merchant'=>$this->merchant_menus_tree]);
    }

    /**
     * 添加权限.
     *
     * @return \think\Response
     */
    public function add()
    {
        if(\request()->isPost()){
            $pid = input('post.pid',0,'intval');
            $name = input('post.name','','trim');
            $sort = input('post.sort');
            $module = input('post.module','','trim');
            $action = input('post.action','','trim');
            $identification = input('post.identification','','trim');
            $data = [
                'fid' => $pid,
                'name' => $name,
                'sort' => $sort,
                'module' => $module,
                'action' => $action,
                'identification' => $identification,
                'show' => config('code.show_true')
            ];
            $validate = validate('MerchantMenu');
            if($pid == 0){
                if(!$validate->scene('topadd')->check($data)){
                    $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$validate->getError());
                }else{
                    //顶级权限
                    $id = model('MerchantMenu')->add($data);
                    if($id){
                        Cache::store('redis')->rm(config('redis.xm_menus_all'));
                        $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'成功添加');
                    }else{
                        $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'添加失败');
                    }
                }
            }else{
                if(!$validate->scene('add')->check($data)){
                    $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$validate->getError());
                }else{
                    $id = model('Menu')->add($data);
                    if($id){
                        Cache::store('redis')->rm(config('redis.xm_menus_all'));
                        $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'成功添加');
                    }else{
                        $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'添加失败');
                    }
                }
            }
        }else{
            $fid = input('param.fid');
            if(empty($fid)){
                return '参数错误';
            }
            return $this->fetch('',['fid'=>$fid]);
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
